import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
    state:{//存放状态
        favorite: uni.getStorageSync("dg_favorite") || [],
    },
	mutations:{
		favoriteAdd: function(state, payload) {
				console.log(state.favorite);
				state.favorite = [payload.newCook, ...state.favorite];
				// window.localStorage.setItem('dg_favorite', JSON.stringify(state.favorite))
				uni.setStorageSync('dg_favorite', state.favorite);
			},
		favoriteReduce: function(state, payload) {			
		  if(payload.id){
			var sy_index = state.favorite.findIndex( item => item.cook_id == payload.id);
			console.log(sy_index)
			state.favorite.splice(sy_index,1);
		  }
		  else{
			var sc_index = payload.index;
			state.favorite.splice(sc_index,1);
		  }
			// window.localStorage.setItem('dg_favorite', JSON.stringify(state.favorite))
			uni.setStorageSync('dg_favorite', state.favorite);
		},
	}
})
export default store